/**
 * Odontosis - (2008 - Eclipse 3.3 - JDK1.6 Update 3) 
 */
package com.odontosis.dao;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.odontosis.as.OdontosisDataAccessObject;
import com.odontosis.entidade.GrupoUsuario;
import com.odontosis.entidade.Usuario;
import com.odontosis.util.HibernateUtil;
import com.odontosis.util.StringUtilsOdontosis;

/**
 * @author Oto ltima modificao: $Author$<br>
 *         Verso: $Revision$ $Date$
 */
public class UsuarioDAO extends OdontosisDataAccessObject<Usuario> {


	public Usuario buscarPorLogin(String login){
		Session session = HibernateUtil.currentSession(); 
		Criteria criteria = session.createCriteria(Usuario.class);
		criteria.add(Restrictions.eq("login", login));
		criteria.add(Restrictions.eq("ativo", true));
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		return (Usuario) criteria.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Usuario> pesquisarPorNomeLoginGrupo(String nome, String login, GrupoUsuario grupo){
		Session session = HibernateUtil.currentSession();
		Criteria criteria = session.createCriteria(Usuario.class);
		if(!StringUtilsOdontosis.isVazia(login)){
			criteria.add(Restrictions.like("login", login, MatchMode.ANYWHERE));
		}
		if(!StringUtilsOdontosis.isVazia(nome)){
			criteria.add(Restrictions.like("nome", nome, MatchMode.ANYWHERE));
		}
		if(grupo != null){
			criteria.add(Restrictions.eq("grupo", grupo));
		}
		criteria.add(Restrictions.eq("ativo", true));
		criteria.addOrder(Order.asc("nome"));
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		return criteria.list();
	}
	
}
